//
//  ALPTabBarViewController.m
//  AlpFM
//
//  Created by KING on 16/1/26.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPTabBarViewController.h"
#import "ALPTabButton.h"
#import "ALPPlayViewController.h"
#import "ALPSingleModel.h"
#import "ALPDateBaseManager.h"
#import "UIButton+WebCache.h"
@interface ALPTabBarViewController ()
@property (nonatomic, retain) ALPTabButton *previousBtn;
@property(nonatomic,retain)UIView *bgView;
@property (nonatomic, retain)UIImageView *myImage;
@property (nonatomic, retain)UIImageView *myImageView;
@property (nonatomic, retain)UIImageView *myImageThree;
@property (nonatomic, retain)UIImageView *myImageTwo;
@end

#define WIDTH  [[UIScreen mainScreen] bounds].size.width
#define HEIGHT [[UIScreen mainScreen] bounds].size.height


@implementation ALPTabBarViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //如果是播放状态,就开转
    if ([ALPPlayViewController sharePlayerViewController].player.state == STKAudioPlayerStatePlaying) {
//        NSLog(@"zhuan");
        [self.playButton startCircle];
        //隐藏播放按钮
        self.playButton.playBtn.alpha = 0;
        self.playButton.playBtn.enabled = NO;
        //如果是暂停状态,就不转
    } else if ([ALPPlayViewController sharePlayerViewController].player.state == STKAudioPlayerStatePaused) {
        [self.playButton stopCircle];
    } else {
    }
    //建立一个按钮改变图片的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changImage:) name:@"changeImage" object:nil];
}

#pragma mark - 通知接收 改变按钮的图片
- (void)changImage:(NSNotification *)notification{
    
    [self.playButton.musicBtn sd_setBackgroundImageWithURL:notification.object forState:UIControlStateNormal];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    self.tabBarImageView = [[UIImageView alloc] init];
    self.tabBarImageView.backgroundColor = [UIColor whiteColor];
    //开启交互
    self.tabBarImageView.userInteractionEnabled = YES;
    //自定义设置tabbar是否隐藏
    [self isHiddenTabBar:NO withViewController:nil];
    
    //掉用自定义生成按钮的方法
    [self creatButtonWithNormalName:@"tabbar_find_n@2x.png" andSelectName:@"tabbar_find_h@3x.png"andTitle:@""andIndex:0];
    
    [self creatButtonWithNormalName:@"tabbar_sound_n@3x.png"andSelectName:@"tabbar_sound_h@2x.png"andTitle:@""andIndex:1];
    
    [self creatButtonWithNormalName:@"tabbar_download_n@3x.png"andSelectName:@"tabbar_download_h@3x.png"andTitle:@""andIndex:2];
    
    [self creatButtonWithNormalName:@"tabbar_me_n@3x.png"andSelectName:@"tabbar_me_h@2x.png"andTitle:@""andIndex:3];
    
    //默认选中第一个按钮
    ALPTabButton *btn = self.tabBarImageView.subviews[0];
    [self changeViewController:btn];
    
    //圆按钮(view)这个按钮用得是播放按钮创建的
    self.playButton = [[ALPPlayButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH / 5, SCREEN_WIDTH / 5)];
    [self.tabBarImageView addSubview:self.playButton];
    [self.playButton.musicBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

}
#pragma mark - 点击播放按钮会模态出播放页面
- (void)buttonAction:(UIButton *)btn{
//    NSLog(@"会弹出播放页");
    ALPPlayViewController *player = [ALPPlayViewController sharePlayerViewController];
//    UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:player];
    
    [self presentViewController:player animated:YES completion:^{
        
    }];
}
#pragma mark - 自定义设置tabbar是否隐藏
- (void)isHiddenTabBar:(BOOL)hidden withViewController:(UIViewController *)viewController{
    //先删除自己定义的
    [self.tabBarImageView removeFromSuperview];
    
    if (hidden) {//如果是隐藏状态
        [self.tabBar setHidden:NO];
        [self.tabBarImageView setFrame:self.tabBar.bounds];
        //将自定义的TabBar添加到系统的taBbar上
        [self.tabBar addSubview:self.tabBarImageView];
        if (viewController != nil) {
            //推到下一页会隐藏tabBar
            viewController.hidesBottomBarWhenPushed = YES;
        }
    } else {//如果不是隐藏状态
        [self.tabBar setHidden:YES];
        [self.tabBarImageView setFrame:self.tabBar.frame];
        //将自定义的tabBar添加到View上
        [self.view addSubview:self.tabBarImageView];
        
    }
    
}
//#pragma mark - 创建按钮通用方法
//- ( void )creatButtonWithNormalName:( NSString *)normal andSelectName:( NSString *)selected andTitle:( NSString *)title andIndex:( int )index
//{
//    
//    HFCustomButton *button = [HFCustomButton buttonWithType: UIButtonTypeCustom];
//    button.tag = index;
//    
//    CGFloat buttonW = self.tabBarImageView.frame.size.width / 5;
//    NSLog(@"button多宽啊%lf", buttonW*5);
//    CGFloat buttonH = self.tabBarImageView.frame.size.height;
//    
//    button.frame = CGRectMake (SCREEN_WIDTH / 5 * index, 0 , buttonW, buttonH);
//    
//    //    NSLog(@"QQ%lf, %lf, %lf, %lf", button.frame.origin.x, button.frame.origin.y, button.frame.size.width, button.frame.size.width);
//    //设置按钮的图片样式
//    [button setImage :[UIImage imageNamed:normal] forState:UIControlStateNormal];
//    [button setImage :[UIImage imageNamed:selected] forState : UIControlStateDisabled];
//    
//    [button setTitle:title forState:UIControlStateNormal];
//    
//    [button addTarget:self action:@selector (changeViewController:) forControlEvents : UIControlEventTouchDown];
//    
//    //    // 让图片在按钮内居中
//    //    button.imageView.contentMode = UIViewContentModeCenter ;
//    //    // 让标题在按钮内居中
//    //    button.titleLabel.textAlignment = NSTextAlignmentCenter ;
//    //    // 设置标题的字体大小
//    //    button.titleLabel.font = [ UIFont systemFontOfSize:12];
//    
//    [self.tabBarImageView addSubview:button];
//    
//    
//    
//    
//}

#pragma mark - 按钮动作方法 切换页面
- (void)changeViewController:(ALPTabButton *)btn{
    //    NSLog(@"执行了嘛");
    //设置选中的控制器,用来切换不同的界面(系统的)
    self.selectedIndex = btn.tag;
    //让选中的按钮只执行一次(变为不可用)
    btn.enabled = NO;
    //如果以前的btn 不是现在点击的btn
    if (self.previousBtn != btn) {
        //变为可用
        self.previousBtn.enabled = YES;
    }
    //记录当前的btn
    self.previousBtn = btn;
    
}





#pragma mark 创建按钮通用方法
- ( void )creatButtonWithNormalName:( NSString *)normal andSelectName:( NSString *)selected andTitle:( NSString *)title andIndex:( int )index

{
    
    ALPTabButton *button = [ALPTabButton buttonWithType:UIButtonTypeCustom ];
    
    button. tag = index;
    
    CGFloat buttonW = self.tabBarImageView.frame.size.width / 6;
    
    CGFloat buttonH = self.tabBarImageView.frame.size.height;
    if (index < 2) {
        button. frame = CGRectMake ( 80 *index, 0 , buttonW, buttonH);
    }
    if (index > 1) {
        button. frame = CGRectMake (self.view.bounds.size.width * 0.6 + 80* (index-2) , 0 , buttonW, buttonH);
    }
    
    [button setImage :[UIImage imageNamed:normal] forState:UIControlStateNormal];
    
    [button setImage :[UIImage imageNamed:selected] forState : UIControlStateDisabled];//设置按钮无效时的图片样式
    
    [button setTitle:title forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector (changeViewController:) forControlEvents : UIControlEventTouchDown];
    
    button.imageView.contentMode = UIViewContentModeCenter ; // 让图片在按钮内居中
    //    button.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
    button.titleLabel.textAlignment = NSTextAlignmentCenter ; // 让标题在按钮内居中
    
    button.titleLabel.font = [ UIFont systemFontOfSize:12]; // 设置标题的字体大小
    
    [self.tabBarImageView addSubview:button];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
